import java.io.*;
import java.util.Scanner;

public class IOTest {
    public static void main(String[] args) throws IOException {
        while (true) {
            System.out.println("请选择方法：（1-12）");
            Scanner scanner = new Scanner(System.in);
            String input = scanner.nextLine();
            if (input.equals("1")) {
                test01();
            }
            if (input.equals("2")) {
                test02();
            }
            if (input.equals("3")) {
                test03();
            }
            if (input.equals("31")) {
                test31();
            }
            if (input.equals("4")) {
                test04();
            }
            if (input.equals("5")) {
                test05();
            }
            if (input.equals("6")) {
                test06();
            }
            if (input.equals("7")) {
                test07();
            }
            if (input.equals("8")) {
                test08();
            }
            if (input.equals("9")) {
                test09();
            }
            if (input.equals("10")) {
                test10();
            }
            if (input.equals("11")) {
                test11();
            }
            if (input.equals("12")) {
                test12();
            }
            if (input.equals("13")) {
                test13();
            }
            if (input.equals("14")) {
                test14();
            }
            if (input.equals("15")) {
                test15();
            }
        }
    }

    public static void test01() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("方法1-请输入一个字符");
        char c;
        c = (char) bufferedReader.read();
        System.out.println("你输入的字符为" + c);
    }

    public static void test02() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("方法2-请输入一个字符，按 q 键结束");
        char c;
        do {
            c = (char) bufferedReader.read();
            System.out.println("你输入的字符为" + c);
        } while (c != 'q');
    }

    public static void test03() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("方法3-请输入一行字符");
        String str = bufferedReader.readLine();
        System.out.println("你输入的字符为" + str);
    }

    public static void test31() throws IOException {
        System.out.println("方法16-请输入：");
        InputStream inputStream = System.in;
        while (true) {
            System.out.println(inputStream.read());
        }
    }

    public static void test04() throws IOException {
        System.out.println("方法4");
        byte[] bytes = {0, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 126, -127, 126};
        FileOutputStream fileOutputStream = new FileOutputStream(new File("").getAbsolutePath() + "/io/test.txt");
        // 写入二进制文件
        fileOutputStream.write(bytes);
        fileOutputStream.close();
    }


    public static void test05() throws IOException {
        System.out.println("方法5");
        FileInputStream fileInputStream = new FileInputStream(new File("").getAbsolutePath() + "/io/test.txt");
        int c;
        // 读取写入的二进制文件，输出字节数组
        while ((c = fileInputStream.read()) != -1) {
            System.out.printf("%d ", c);
        }
    }

    public static void test06() throws IOException {
        System.out.println("方法6");
        FileWriter fileWriter = new FileWriter(new File("").getAbsolutePath() + "/io/test.txt");
        fileWriter.write("Hello，world！\n welcome \n");
        fileWriter.write("不会覆盖文件原本的内容\n");
//        fileWriter.write(null); 不能直接写入 null
        fileWriter.append("append\n");
        fileWriter.append(null);
        fileWriter.flush();
        System.out.println("文件的默认编码为" + fileWriter.getEncoding());
        fileWriter.close();
    }

    public static void test07() throws IOException {
        System.out.println("方法7");
        FileWriter fileWriter = new FileWriter(new File("").getAbsolutePath() + "/io/test.txt", false); // 关闭追加模式，变为覆盖模式
        fileWriter.write("Hello，world！welcome\n");
        fileWriter.write("我来覆盖文件原本的内容");
        fileWriter.append("我是下一行");
        fileWriter.flush();
        System.out.println("文件的默认编码为" + fileWriter.getEncoding());
        fileWriter.close();
    }

    public static void test08() throws IOException {
        System.out.println("方法8");
        FileReader fileReader = new FileReader(new File("").getAbsolutePath() + "/io/test.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String str;
        while ((str = bufferedReader.readLine()) != null) {
            System.out.println(str);
        }
        fileReader.close();
        bufferedReader.close();
    }

    public static void test09() throws IOException {
        System.out.println("方法9");
        FileReader fileReader = new FileReader(new File("").getAbsolutePath() + "/io/test.txt");
        int c;
        while ((c = fileReader.read()) != -1) {
            System.out.print((char) c);
        }
    }

    public static void test10() throws IOException {
        System.out.println("方法10");
        FileOutputStream fileOutputStream = new FileOutputStream(new File("").getAbsolutePath() + "/io/test2.txt");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "GBK"); // 使用 GBK 编码文件
        outputStreamWriter.write("Hello，world！\nwelcome\n");
        outputStreamWriter.append("另外一行内容");
        outputStreamWriter.flush();
        System.out.println("文件的编码为" + outputStreamWriter.getEncoding());
        outputStreamWriter.close();
        fileOutputStream.close();
    }

    public static void test11() throws IOException {
        System.out.println("方法11");
        FileInputStream fileInputStream = new FileInputStream(new File("").getAbsolutePath() + "/io/test2.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK"); // 使用 GBK 解码文件
        System.out.println("文件编码为：" + inputStreamReader.getEncoding());
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String str;
        while ((str = bufferedReader.readLine()) != null) {
            System.out.println(str);
        }
        bufferedReader.close();
        inputStreamReader.close();
    }

    public static void test12() throws IOException {
        // 输入和输出都使用缓冲流
        String filename = new Scanner(System.in).nextLine().trim();
        FileInputStream file = new FileInputStream(filename);
        File f = new File(filename);

        BufferedInputStream inBuffer = new BufferedInputStream(file);
        filename = f.getName();
        FileOutputStream out = new FileOutputStream(filename);
        BufferedOutputStream outBuffer = new BufferedOutputStream(out);
        int len = 0;
        byte[] bs = new byte[1024];
        long begin = System.currentTimeMillis();
        while ((len = inBuffer.read(bs)) != -1) {
            outBuffer.write(bs, 0, len);
        }
        System.out.println("复制文件所需的时间：" + (System.currentTimeMillis() - begin));
        inBuffer.close();
        file.close();
        outBuffer.close();
        out.close();
    }


    public static void test13() throws IOException {
        // 输入和输出都使用缓冲流
        String filename = new Scanner(System.in).nextLine().trim();
        FileInputStream in = new FileInputStream(filename);
        File f = new File(filename);
        BufferedInputStream inBuffer = new BufferedInputStream(in);
        FileOutputStream out = new FileOutputStream(f.getName());
        int len = 0;
        byte[] bs = new byte[1024];
        long begin = System.currentTimeMillis();
        while ((len = inBuffer.read(bs)) != -1) {
            out.write(bs, 0, len);
        }
        System.out.println("复制文件所需时间：" + (System.currentTimeMillis() - begin));
        inBuffer.close();
        in.close();
        out.close();
    }

    public static void test14() throws IOException {
        // 输入和输出都不使用缓冲流

        String filename = new Scanner(System.in).nextLine().trim();
        FileInputStream in = new FileInputStream(filename);
        File f = new File(filename);
        FileOutputStream out = new FileOutputStream(f.getName());
        int len = 0;
        byte[] bs = new byte[1024];
        long begin = System.currentTimeMillis();
        while ((len = in.read(bs)) != -1) {
            out.write(bs, 0, len);
        }
        System.out.println("复制文件所需时间：" + (System.currentTimeMillis() - begin));
        in.close();
        out.close();
    }

    public static void test15() throws IOException {
        // 不使用缓冲
        String filename = new Scanner(System.in).nextLine().trim();
        FileInputStream in = new FileInputStream(filename);
        File f = new File(filename);
        FileOutputStream out = new FileOutputStream(f.getName());
        int len = 0;
        long begin = System.currentTimeMillis();
        while ((len = in.read()) != -1) {
            out.write(len);
        }
        System.out.println("复制文件所需时间：" + (System.currentTimeMillis() - begin));
        in.close();
        out.close();
    }
}
